﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ExcellOnServices.Models;
using ExcellOnServices.Mailers;
using System.Collections;
using PagedList;
using ExcellOnServices.Models.Utils;

namespace ExcellOnServices.Areas.Admin.Controllers
{
    public class CompanyController : Controller
    {
        private ExcellOnServicesContext db = new ExcellOnServicesContext();

        // GET: /Admin/Company/
        [Authorize]
        public ActionResult Index(string search = null, string status = null, int page = 1)
        {
            var accounts_company = db.Accounts_Company.Select(a => a);

            if (!String.IsNullOrEmpty(search))
            {
                accounts_company = accounts_company.Where(s => s.BrandName.ToUpper().Contains(search.ToUpper()));
            }
            if (!String.IsNullOrEmpty(status))
            {
                int statusID = (int)Enum.Parse(typeof(Accounts_Company.StatusType), status);
                accounts_company = accounts_company.Where(s => (int)s.Status == statusID);
            }
            IEnumerable<Accounts_Company.StatusType> statusList = Enum.GetValues(typeof(Accounts_Company.StatusType)).OfType<Accounts_Company.StatusType>();

            ViewBag.Status = new SelectList(statusList);
            ViewBag.StatusName = status;
            ViewBag.SearchString = search;
            ViewBag.Page = page;

            return View(accounts_company.OrderBy(s => s.Status).Select(s => s).ToPagedList(page, 3));
        }

        [Authorize]
        public ActionResult SetActive(int? id, string search, string status, int? page)
        {
            string passNotEncrypted;
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Accounts_Company accounts_company = db.Accounts_Company.Find(id);
            if (accounts_company == null)
            {
                return HttpNotFound();
            }
            else
            {
                accounts_company.Account.Password = RandomPassword.Generate(6, 8);
                passNotEncrypted = accounts_company.Account.Password;
                accounts_company.Account.Password = EncryptionHelper.encrypPassMD5(accounts_company.Account.Password);

                accounts_company.Status = Accounts_Company.StatusType.Activated;//Accounts_Company.State.Activated.GetHashCode();
                db.SaveChanges();

                IMailer mailer = new Mailer();
                mailer.ActiveCompanyAccMail(accounts_company, passNotEncrypted).Send();

                return RedirectToAction("Index", new {search = search, status = status, page = page});
            }
        }
        [Authorize]
        public ActionResult DeleteRequest(int? id, string search, string status, int? page)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Accounts_Company accounts_company = db.Accounts_Company.Find(id);
            if (accounts_company == null)
            {
                return HttpNotFound();
            }
            else
            {
                db.Accounts_Company.Remove(accounts_company);
                db.SaveChanges();

                return RedirectToAction("Index", new { search = search, status = status, page = page });
            }
        }


        // GET: /Admin/Company/Details/5
        [Authorize]
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Accounts_Company accounts_company = db.Accounts_Company.Find(id);
            if (accounts_company == null)
            {
                return HttpNotFound();
            }
            return View(accounts_company);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
